﻿package njClass.base
{
	public class TimePlus
	{
		//把PHP传输的YYYY-MM-DD-HH-MM-SS格式的时间转换为AS内的Date类型
		public static function PHPdateToAs(date:String):Date{
			var index_num0:int=date.indexOf("-");
			var YY:int=int(date.substring(0,index_num0));
			var index_num1:int=date.indexOf("-",index_num0+1);
			var MM:int=int(date.substring(index_num0+1,index_num1));
			index_num0=date.indexOf("-",index_num1+1);
			var DD:int=int(date.substring(index_num1+1,index_num0));
			index_num1=date.indexOf("-",index_num0+1);
			var hh:int=int(date.substring(index_num0+1,index_num1));
			index_num0=date.indexOf("-",index_num1+1);
			var mm:int=int(date.substring(index_num1+1,index_num0));
			var ss:int=int(date.substring(index_num0+1));
			var Redate:Date=new Date(YY,MM-1,DD,hh,mm,ss);
			return Redate;
		}
		
		//date2与date1的时间差，date2为空则表示date1与当前时间的时间差，date2应该新于date1
		public static function TimeSub(date1:Date,date2:Date=null):Array {
			var str:String="";
			if(date2==null){
				date2=new Date();
			}
			//trace(date2.getTime()-date1.getTime());
			var num:Number=date2.getTime()-date1.getTime();
			if (num<=0) {
				return null;
			}
			var dDD:Number=Math.floor(num/(1000*60*60*24));
			var dhh:Number=Math.floor((num-dDD*(1000*60*60*24))/(1000*60*60));
			var dmm:Number=Math.floor((num-dDD*(1000*60*60*24)-dhh*(1000*60*60))/(1000*60));
			var dss:Number=Math.floor((num-dDD*(1000*60*60*24)-dhh*(1000*60*60)-dmm*(1000*60))/1000);
			/*if (dDD!=0) {
				str+=dDD + "天";
			}
			if (dhh!=0) {
				str+=dhh + "小时";
			}
			if (dss>=10) {
				str+=dmm + ":" + dss;
			} else {
				str+=dmm + ":0" + dss;
			}*/
			return [dDD,dhh,dmm,dss];
		}
	}
}